"""
title:收货地址页面
author:何宗香
data:2022/1/7
"""
import time
from Common.Base import Base, open_browser
from Page.LoginPage import LoginPage, login_url
from time import sleep


class AddressPage(Base):
    """封装表现层: 制作定位器"""
    # 用户中心
    user_center_loc = ('css selector', 'div[class="fl cart-yh"]>a')
    # 收货地址
    address_loc = ('css selector', 'div[class="userMenu"]>a:nth-of-type(4)')
    province = ('css selector', '#selProvinces_0 > option:nth-child(4)')
    def address_locator(self, index='last()'):
        """"""
        # 省市区
        provinces_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[2]')
        cities_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[3]')
        districts_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr/td[2]/select[4]')
        # 收货人姓名
        consignee_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[2]/td[2]/input')
        # 邮箱
        email_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[2]/td[4]/input')
        # 详细地址
        address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[3]/td[2]/input')
        # 邮编
        zipcode_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[3]/td[4]/input')
        # 电话
        telephone_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[4]/td[2]/input')
        # 手机
        mobile_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[4]/td[4]/input')
        # 确认修改
        update_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input')
        # 删除
        delete_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input[2]')
        # 新增收货地址
        insert_address_loc = ('xpath', f'//form[@name="theForm"][{index}]/table/tbody/tr[5]/td[2]/input')

        if index == 'last()':
            return {'provinces_loc': provinces_loc,  # 省
                    'cities_loc': cities_loc,  # 市
                    'districts_loc': districts_loc,  # 区
                    'consignee_loc': consignee_loc,  # 收货人姓名
                    'email_loc': email_loc,  # 收货人邮箱
                    'address_loc': address_loc,  # 详细地址
                    'zipcode_loc': zipcode_loc,  # 邮编
                    'telephone_loc': telephone_loc,  # 电话号码
                    'mobile_loc': mobile_loc,  # 手机号码
                    'insert_address_loc': insert_address_loc  # 新增收货地址按钮
                    }
        else:
            return {'provinces_loc': provinces_loc,  # 省
                    'cities_loc': cities_loc,  # 市
                    'districts_loc': districts_loc,  # 区
                    'consignee_loc': consignee_loc,  # 收货人姓名
                    'email_loc': email_loc,  # 收货人邮箱
                    'address_loc': address_loc,  # 详细地址
                    'zipcode_loc': zipcode_loc,  # 邮编
                    'telephone_loc': telephone_loc,  # 电话号码
                    'mobile_loc': mobile_loc,  # 手机号码
                    'update_address_loc': update_address_loc,  # 修改收货地址
                    'delete_address_loc': delete_address_loc,  # 删除收货地址
                    }

    def input_consignee(self, name, index='last()'):
        """输入收货人姓名"""
        self.send_keys(self.address_locator(index)['consignee_loc'], name)

    def delete_address(self, index='1'):
        """删除收货地址"""
        self.click(self.address_locator(index)['delete_address_loc'])

    def click_user_center(self):
        """点击用户中心"""
        self.click(self.user_center_loc)

    def click_address(self):
        """点击收货地址"""
        self.click(self.address_loc)

    def input_province(self, province, index='last()'):
        """省份"""
        self.drop_down_list(self.address_locator(index)['provinces_loc'], province)

    def input_city(self, city, index='last()'):
        """城市"""
        self.drop_down_list(self.address_locator(index)['cities_loc'], city)

    def input_district(self, district, index='last()'):
        """地区"""
        self.drop_down_list(self.address_locator(index)['districts_loc'], district)

    def input_particular_address(self, address, index='last()'):
        """输入详细地址"""
        self.send_keys(self.address_locator(index)['address_loc'], address)

    def input_tel(self, tel, index='last()'):
        """输入电话号码"""
        self.send_keys(self.address_locator(index)['telephone_loc'], tel)

    def input_email(self, email, index='last()'):
        """输入电子邮件地址"""
        self.send_keys(self.address_locator(index)['email_loc'], email)

    def click_add_adress(self, index='last()'):
        """点击新增收货地址"""
        self.click(self.address_locator(index)['insert_address_loc'])

    def click_update(self, index='last()'):
        """点击修改收货地址"""
        self.click(self.address_locator(index)['update_address_loc'])

    def confirm(self, option):
        """确认是否删除"""
        if option == '确定':
            self.switch_alert('accept')
        elif option == '取消':
            self.switch_alert('quit')

    def is_update_success(self, province):
        """判断是否修改成功"""
        return self.is_text_equal_in_element(self.province, province)

    def gain_value(self, loc, index='last()'):
        """获取元素value值"""

        return self.find_element(self.address_locator(index)[loc]).get_attribute('value')




if __name__ == '__main__':
    driver = open_browser()
    login = LoginPage(driver)
    login.open_url(login_url)
    login.input_username('root1')
    login.input_password('123456')
    login.click_login()
    address = AddressPage(driver)
    address.click_user_center()
    address.click_address()
    address.input_consignee('root1')
    address.input_province('四川省')
    sleep(3)
    address.input_city('成都市')
    sleep(3)
    address.input_district('锦江区')
    address.input_tel('1234567')
    address.input_particular_address('春熙路')
    address.input_email('740739419@qq.com')
    address.click_add_adress()
    # address.delete_address()
